


#include "Skill.h"
#include "Campaign.h"
#include "Attribute.h"
#include "XmlDefines.h"



Skill::Skill(QObject* parent) : Item(d20Type_Skill,parent)
{
    data = new Data;
}



Skill::Skill(const Skill& other) : Item(other)
{
    setAttribute(other.attribute());
    setClassSkill(other.isClassSkill());

    setRank(other.rank());
}



QVariant Skill::bonus()
{
    Attribute* a = Campaign::instance(campaign())->attribute(attribute());
    if (!a) return QVariant();

    return rank() + a->bonus().toInt();
}



int Skill::rank() const
{
    if (value().isNull() || !value().canConvert(QVariant::Int)) return 0;

    return value().toInt();
}



bool Skill::isClassSkill() const
{

    return data->class_skill;
}



QString Skill::campaign() const
{
    return data->campaign;
}



QString Skill::attribute() const
{
    return data->attribute;
}



void Skill::setRank(const int& rank)
{
    setValue(rank);
}



void Skill::setClassSkill(const bool& yes)
{
    if (data->class_skill == yes) return;
    data->class_skill = yes;
    emit sigChanged();
}



void Skill::setCampaign(const QString &campaign)
{
    if (data->campaign == campaign) return;
    data->campaign = campaign;
    emit sigChanged();
}



void Skill::setAttribute(const QString& attribute)
{
    if (data->attribute == attribute) return;
    data->attribute = attribute;
    emit sigChanged();
}



QDomElement Skill::toXmlElem(QDomElement& elem)
{
    elem = Item::toXmlElem(elem);

    elem.setAttribute(D20_CLASS_SKILL, isClassSkill());

    elem.setAttribute(D20_CAMPAIGN.toLower(), campaign());
    elem.setAttribute(D20_ATTRIBUTE.toLower(), attribute());
    return elem;
}



void Skill::fromXmlElem(QDomElement& elem)
{
    Item::fromXmlElem(elem);

    setCampaign(elem.attribute(D20_CAMPAIGN.toLower(), QString::null));
    setAttribute(elem.attribute(D20_ATTRIBUTE.toLower(), QString::null));
    setClassSkill(QVariant(elem.attribute(D20_CLASS_SKILL, "false")).toBool());
}
